﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Collections;

namespace MediaCenter
{
    public partial class StartUp : Form
    {
        string[] pathsMovies;
        string[] pathsSeries;
        string[] pathsMusic;

        private GetSerier.GetSerier serier;
        private GetMusic.GetMusic musikk;
        private GetMovies.GetMovies movies;

        private string pwd;

        public StartUp()
        {
            //pathsMovies = getXMLData("Test");
            pathsMovies = getXMLData("pathFilmer");
            pathsSeries = getXMLData("pathSerier");
            pathsMusic = getXMLData("pathMusikk");

            movies = new GetMovies.GetMovies(pathsMovies);
            serier = new GetSerier.GetSerier(pathsSeries);
            musikk = new GetMusic.GetMusic(pathsMusic);

            InitializeComponent();

            fillUpMovies();
            fillUpSerier();
            fillUpMusic();
        }

        private string[] getXMLData(string input)
        {
            string[] temp = ConfigurationManager.AppSettings.GetValues(input);

            var split = temp[0].Split(',');
            int i = 0;
            var type = new string[split.Length];
            foreach (string p in split)
            {
                Console.WriteLine(p);
                type[i] = split[i];
                i++;
            }
            return type;
        }

        public void fillUpSerier()
        {
            foreach (string f in serier.getDir())
            {

                listBoxSeries.Items.Add(f.Substring(f.LastIndexOf('\\') + 1));
                //listBoxSeries.Items.Add(f);
            }

        }

        public void fillUpMusic()
        {
            foreach (string m in musikk.getDir())
            {
                listBoxMusic.Items.Add(m);
            }
        }

        public void fillUpMovies() 
        {
            foreach (GetMovies.Movie f in movies.getMovieList()) 
            {
                listBoxMovies.Items.Add(f.getName());
            }
        }

        private void btn_Spotify_Click(object sender, EventArgs e)
        {
            new StartSpotify().lauchSpotify();
        }

        private void btn_AmbiLight_Click(object sender, EventArgs e)
        {
            new UseLed().Show();
        }

        private void StartUp_Load(object sender, EventArgs e)
        {

        }

        private void listBoxSeries_SelectedIndexChanged(object sender, EventArgs e)
        {
            int SelectedIndex = listBoxSeries.SelectedIndex;

            string[] temp = new string[listBoxSeries.Items.Count];
            listBoxSeries.Items.CopyTo(temp, 0);
            

            foreach (string f in serier.getDir())
                if (f.EndsWith(temp[SelectedIndex])){
                    pwd = f;
                    break;
                }
            if(!pwd.EndsWith(temp[SelectedIndex]))
                pwd = pwd + "\\" + temp[SelectedIndex];

            serier.setWinList(temp);
            Console.WriteLine(pwd);

            listBoxSeries.Items.Clear();

            if (SelectedIndex == -1)
                SelectedIndex = 0;

            try
            {
                foreach (string f in serier.getSubDir(pwd))
                    listBoxSeries.Items.Add(f.Substring(f.LastIndexOf('\\') + 1));

               foreach (string f in serier.getEpisoder(pwd))
                    //listBoxSeries.Items.Add(f);
                    listBoxSeries.Items.Add(f.Substring(f.LastIndexOf('\\') + 1));
            }
            catch (NullReferenceException){
                new StartMovie().watchMovie(pwd);
                listBoxSeries.Items.Clear();
                foreach (string f in serier.getDir())
                {
                   listBoxSeries.Items.Add(f.Substring(f.LastIndexOf('\\') + 1));
                }
            }
        }

        private void listBoxMovies_SelectedIndexChanged(object sender, EventArgs e)
        {
            contextMenuStrip1.Show();
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            pwd = "";
            //  if (menuTabControl.SelectedTab.Equals("Serier")) {
            listBoxSeries.Items.Clear();
            foreach (string f in serier.getDir())
            {
                listBoxSeries.Items.Add(f.Substring(f.LastIndexOf('\\') + 1));
               // listBoxSeries.Items.Add(f);
                //     }
            }
            if (menuTabControl.SelectedTab.Equals("Musikk"))
            {
                listBoxMusic.Items.Clear();
                foreach (string m in musikk.getDir())
                {
                    listBoxMusic.Items.Add(m);
                }
            }
        }

        private void listBoxMusic_SelectedIndexChanged(object sender, EventArgs e)
        {
            int SelectedIndex = listBoxMusic.SelectedIndex;

            string[] temp = new string[listBoxMusic.Items.Count];
            listBoxMusic.Items.CopyTo(temp, 0);
            musikk.setWinList(temp);

            listBoxMusic.Items.Clear();

            if (SelectedIndex == -1)
                SelectedIndex = 0;
            try
            {
                foreach (string f in musikk.getSubDir(temp[SelectedIndex]))
                    listBoxMusic.Items.Add(f);

                foreach (string f in musikk.getSanger(temp[SelectedIndex]))
                    listBoxMusic.Items.Add(f);
            }
            catch (NullReferenceException)
            {
                new StartMovie().watchMovie(temp[SelectedIndex]);
                listBoxMusic.Items.Clear();
                foreach (string f in musikk.getDir())
                {
                    listBoxSeries.Items.Add(f);
                }
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void settingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Options().Show();
        }

        private void pLAYToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new StartMovie().watchMovie(movies.getMovieList().ElementAt(listBoxMovies.SelectedIndex).getPath());
        }

        private void iMDBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new IMDBInfo(movies.getMovieList().ElementAt(listBoxMovies.SelectedIndex)).Show();
        }

    }
}
